# -*- coding: utf-8 -*-
# !/usr/bin/env python
import uuid

from components.models import KubernetesClusterSign, KubernetesManage
from utils.mysql_utils import query_to_list
from utils.views import ReturnClass


def set_k8s_api_sign(host_id, kubernetes_cluster_id):

    try:
        save_data = KubernetesClusterSign(host_id=host_id, kubernetes_cluster_id=kubernetes_cluster_id)
        save_data.save()
    except Exception as e:
        if e.args[0] == 1062:
            KubernetesClusterSign.objects.filter(kubernetes_cluster_id=kubernetes_cluster_id).update(host_id=host_id)
        else:
            return ReturnClass.return_bust(str(e))

    return ReturnClass.return_succeed("查询成功")


def get_env_k8s_cluster(env_id):
    sql = """
        SELECT * FROM kubernetes_cluster a WHERE a.env_id = {env_id} AND a.`status`= 1 ORDER BY a.kubernetes_cluster_id DESC
        """.format(
        env_id=env_id
    )
    data = query_to_list(sql, [])

    return data

def get_env_k8s_cluster_all():
    sql = """
        SELECT * FROM kubernetes_cluster a WHERE  a.`status`= 1 ORDER BY a.kubernetes_cluster_id DESC
        """
    data = query_to_list(sql, [])

    return data


def get_k8s_cluster_hosts(kubernetes_cluster_id):
    sql = """
    SELECT a.*,b.host_ip,c.sys_code_name AS k8s_roles_type_text, d.kubernetes_cluster_sign_id,e.host_nick
    FROM kubernetes_manage a
    LEFT JOIN hosts_management b ON a.host_id = b.host_id
    LEFT JOIN sys_code c ON c.sys_code = a.kubernetes_role AND c.sys_code_type = "k8s_roles_type"
    LEFT JOIN kubernetes_cluster_sign d ON d.host_id = a.host_id
    LEFT JOIN hosts_management e ON e.host_id = b.host_id 
    WHERE a.kubernetes_cluster_id = {kubernetes_cluster_id} 
    AND e.`status`= 1
    ORDER BY a.kubernetes_cluster_id DESC
        """.format(
        kubernetes_cluster_id=kubernetes_cluster_id
    )
    data = query_to_list(sql, [])

    return data


def get_kubernetes_manage_hosts(host_ids):
    """
    根据【host_ids】：list查询已有主机信息
    @param host_ids:
    @return:
    """
    kubernetes_manage_obj = KubernetesManage.objects.filter(host_id__in=host_ids).values()

    return kubernetes_manage_obj

def save_k8s_cluster_hosts(kubernetes_cluster_id, host_id, deploy_event_ids):
    KubernetesManage.objects.update_or_create(host_id=host_id, kubernetes_cluster_id=kubernetes_cluster_id,
                                              defaults={"host_id": host_id,
                                                        "kubernetes_cluster_id": kubernetes_cluster_id,
                                                        'deploy_event_ids': deploy_event_ids
                                                        })
    return True


def get_config_file_text(kubernetes_cluster_id):
    try:
        sql = """
        SELECT c.* FROM kubernetes_cluster_sign a
        LEFT JOIN kubernetes_cluster b ON a.kubernetes_cluster_id = b.kubernetes_cluster_id
        LEFT JOIN conf_management c ON b.conf_manage_ids = c.conf_manage_ids AND c.conf_manage_code = "kubeconfig"
        WHERE a.kubernetes_cluster_id = {kubernetes_cluster_id}
        """.format(
            kubernetes_cluster_id=kubernetes_cluster_id
        )

        data = query_to_list(sql, [], to_one_dict=True)

        return ReturnClass.return_succeed(data['conf_manage_content'])
    except Exception as e:
        return ReturnClass.return_bust(str(e))

